/*! \file MessageHandlersMap.inl
 *	\brief 
 *
 *	created:	2010/09/09
 *	created:	9:9:2010   16:48
 *	filename: 	s:\DNG-Code\Kiev\Modules\Orbit\Common\MessageHandlersMap.inl
 *	file path:	s:\DNG-Code\Kiev\Modules\Orbit\Common
 *	file base:	MessageHandlersMap
 *	file ext:	inl
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */

namespace tt{
namespace core{

//////////////////////////////////////////////////////////////////////////
template<typename Handler>
bool MessageHandlersMapItem<Handler>::operator<( const MessageHandlersMapItem& other )
{
	return id < other.id;
}

template<typename Handler>
bool MessageHandlersMapItem<Handler>::operator==( const MessageHandlersMapItem& other )
{
	return id == other.id;
}

template<typename Handler>
bool operator<( const MessageHandlersMapItem<Handler>& item, const mg::serverbox::DelimiterType id )
{
	return item.id < id;
}

template<typename Handler>
bool operator<( const mg::serverbox::DelimiterType id, const MessageHandlersMapItem<Handler>& item )
{
	return id < item.id;
}

//////////////////////////////////////////////////////////////////////////
template<typename Handler>
MessageHandlersMapBase<Handler>::MessageHandlersMapBase()
{

}

template<typename Handler>
MessageHandlersMapBase<Handler>::~MessageHandlersMapBase()
{

}

template<typename Handler>
void MessageHandlersMapBase<Handler>::insert( mg::serverbox::DelimiterType id, const Handler& handler )
{
	Items::iterator it = std::lower_bound(m_items.begin(), m_items.end(), id);
	bool handlerExists = (it != m_items.end()) && (it->id == id);
	MG_ASSERT(!handlerExists, "Message handler already exists!!!");
	if (!handlerExists)
	{
		Item item = {id, handler};
		m_items.insert(it, item);
	}
}

//////////////////////////////////////////////////////////////////////////
template<typename Handler>
Handler* MessageHandlersMapBase<Handler>::find( mg::serverbox::DelimiterType id )
{
	Items::iterator it = std::lower_bound(m_items.begin(), m_items.end(), id);
	if ( it != m_items.end() && it->id == id )
		return & it->handler;
	return NULL;
}

//////////////////////////////////////////////////////////////////////////
inline
bool MessageHandlersMap<Functor0>::operator()( mg::serverbox::DelimiterType id)
{
	Items::iterator it = std::lower_bound(m_items.begin(), m_items.end(), id);
	if ( it != m_items.end() && it->id == id )
	{
		(it->handler)();
		return true;
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
template<typename Arg1>
bool MessageHandlersMap< Functor1<Arg1> >::operator()( mg::serverbox::DelimiterType id, Arg1 arg1)
{
	Items::iterator it = std::lower_bound(m_items.begin(), m_items.end(), id);
	if ( it != m_items.end() && it->id == id )
	{
		(it->handler)(arg1);
		return true;
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
template<typename Arg1, typename Arg2>
bool MessageHandlersMap< Functor2<Arg1, Arg2> >::operator()( mg::serverbox::DelimiterType id, Arg1 arg1, Arg2 arg2)
{
	Items::iterator it = std::lower_bound(m_items.begin(), m_items.end(), id);
	if ( it != m_items.end() && it->id == id )
	{
		(it->handler)(arg1, arg2);
		return true;
	}
	return false;
}

} //namespace core
} //namespace tt